<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Shortlist extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Shortlist() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('listingmap/mdl_listingmapmodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('text');
		
		$this->load->model('banners/bannermodel');
	}

	public function index() {
		$this->lang->load('member/wishlist', $this->language);
		$this->load->model('listingmap/mdl_listingmapmodel');
			
//		if (!$this->members->isLogged()) {
//			redirect(base_url() . 'member/login', 'refresh');
//		}
			
		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');
			
		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('heading_title'),
			'href'      => base_url() . 'shortlist'
		);

		$data['module'] = "shortlist";
		$data['view_file'] = "shortlist";
		
		if ($this->input->get('remove')) {
			$key_remove = array_search($this->input->get('remove'), $this->session->userdata('wishlist'));
		
			$wishlist = $this->session->userdata('wishlist');
			unset($wishlist[$key_remove]);
			$this->session->set_userdata('wishlist', $wishlist);
		
			$this->session->set_userdata('success', 'Success: You have modified your wishlist!');
		
			redirect(base_url() . 'shortlist/');
		}
		
		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');
		
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = FALSE;
		}
		
		$this->load->model('wishlist_model');
		
		$options['localorInt'] = NULL;
		$options['market'] = NULL;
		$options['countryDefaultMarket'] = NULL;
		$options['country'] = NULL;
		$options['cities'] = NULL;
		$options['locations'] = NULL;
		$options['subarea'] = NULL;
		$options['list_type'] = NULL;
		$options['propertyfor'] = NULL;
		$options['location'] = NULL;
		$options['propertytype'] = NULL;
		$options['price_min'] = NULL;
		$options['price_max'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['bedroom_min'] = NULL;
		$options['bedroom_max'] = NULL;
		$options['bathroom_min'] = NULL;
		$options['bathroom_max'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "ListedOnUtc DESC";
		$options['features'] = NULL;
		$options['facilities'] = NULL;
		$options['tenu'] = NULL;
		$options['language'] = NULL;
		$options['listing_id'] = NULL;
		
		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		} else {
			$options['page'] = 1;
		}

		$options['offset'] = NULL;
		$options['limit'] = NULL;
		
		$data['listings'] = array();
		
		if ($this->session->userdata('wishlist')) {
			foreach ($this->session->userdata('wishlist') as $key => $listingid) {
				$parts = explode('-', $listingid);
        		$listingid = end($parts);
				$options['listing_id'] = $listingid;
				$listing_info = $this->mdl_listingmapmodel->getProjectListing($options);
				if (isset($listing_info)) {
					$listing_info = $listing_info[0];
		
					$data['listings'][] = array(
					'listingid' => $listing_info['Listing_ID'],
					'name'		=> $listing_info['ProjectName'],
					'price' 	=> $listing_info['Price_Min'],
					'priceUnit'	=> $listing_info['PriceUnit'],
					'size'		=> $listing_info['Size_Min'],
					'sizeUnit'	=> $listing_info['SizeUnit'],
					'beds'		=> $listing_info['Bedroom'],
					'baths'		=> $listing_info['Bathroom'],
					'href' 		=> base_url() . 'detail/' . $listing_info['Link'],
					'remove' 	=> base_url() . 'shortlist.html?remove=' . $listing_info['Listing_ID']
					);
				} else {
					$this->session->unset_userdata($key);
				}
			}
		}
			
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));

		echo Modules::run('template/default_template', $data);
	}

	public function save() {
		if (!$this->members->isLogged()) {
			redirect(base_url() . 'member/login?redirect='. urlencode(base_url().'shortlist'), 'refresh');
		}
			
		$data['module'] = "shortlist";
		$data['view_file'] = "save";
			
		$dataWhere = array(
       		'ShortList' => $this->db->escape($this->session->userdata('wishlist')) ?  serialize($this->session->userdata('wishlist')) : ''
       		);

       		$this->db->where('Member_ID', (int) $this->members->isLogged());
       		$this->db->update('Member', $dataWhere);

       		$data['success'] = 'Success: You have saved your shortlist!';

       		echo Modules::run('template/default_template', $data);
	}

	public function add() {
		$json = array();
		$options = array();

		$wishlist = $this->session->userdata('wishlist');
		if (!$wishlist) {
			$wishlist = array();
		}

		$listingid = $this->input->post('listingid');
		
		$parts = explode('-', $listingid);
        $listingid = end($parts);
		
		if (!isset($listingid)) {
			$listingid = 0;
		}

		$this->load->model('wishlist_model');

		$options['localorInt'] = NULL;
		$options['market'] = NULL;
		$options['countryDefaultMarket'] = NULL;
		$options['country'] = NULL;
		$options['cities'] = NULL;
		$options['locations'] = NULL;
		$options['subarea'] = NULL;
		$options['list_type'] = NULL;
		$options['propertyfor'] = NULL;
		$options['location'] = NULL;
		$options['propertytype'] = NULL;		
		$options['price_min'] = NULL;
		$options['price_max'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['bedroom_min'] = NULL;
		$options['bedroom_max'] = NULL;
		$options['bathroom_min'] = NULL;
		$options['bathroom_max'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "ListedOnUtc DESC";
		$options['features'] = NULL;
		$options['facilities'] = NULL;
		$options['tenu'] = NULL;
		$limit['offset'] = 0;
		$limit['limit'] = 999999;
		$options['orderby'] = NULL;
		$options['language'] = NULL;
		$options['listing_id'] = $listingid;
		
		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		} else {
			$options['page'] = 1;
		}

		$options['offset'] = NULL;
		$options['limit'] = NULL;

		//$listing_info = $this->wishlist_model->getListingWithWhere($options, $limit, $listingid);
		$this->load->model('listingmap/mdl_listingmapmodel');
		$listing_info = $this->mdl_listingmapmodel->getProjectListing($options);
		if (isset($listing_info) && !is_null($listing_info) && count($listing_info) > 0) {
			$listing_info = $listing_info[0];
		}

		if ($listing_info) {
			if (!in_array($this->input->post('listingid'), $wishlist)) {
				array_push($wishlist, $this->input->post('listingid'));
				$this->session->set_userdata(array("wishlist" => $wishlist));
			}

			$json['success'] = sprintf('Success: You have added <a href="%s">%s</a> to your <a href="%s">shortlist</a>!', base_url() . 'detail/' . $this->input->post('listingid'), $listing_info['ProjectName'], base_url() . 'shortlist/');

			$json['total'] = sprintf('Shortlist (%s)', (isset($wishlist) ? count($wishlist) : 0));
		}

		echo json_encode($json);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */